/**
 *
 */
package com.cmcc.awareness;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Scanner;

/**
 * @Date 2018年4月11日 上午10:23:28
 * @author Amish
 *
 */
public class ProtoList {
	
	public HashMap<String,AppInfo> appInfos;//用proto.list文件中的app sub-type name作为key。例如QQ_Media或QQ_IM
	
//	private static ProtoList protoList = new ProtoList();
//    public static ProtoList getInstance(){//饿汉...
//    	return protoList;
//    }
    
    public ProtoList(String protoListPath){
    	appInfos = new HashMap<String,AppInfo>();
    	this.parseFile(protoListPath);
    }
    
    private void parseFile(String protoListPath){
    	
//    	String path = ProtoList.class.getResource("/").getPath()+"config/proto.list";
//    	path = path.replace("%20", " ");
//    	path = path.replace("%23", "#");
    	
    	File file = new File(protoListPath);
		Scanner sc = null;

		try {
			sc = new Scanner(new FileInputStream(file));
			String str = null;
			while (sc.hasNextLine()) {
				str = sc.nextLine();
				if(!"".endsWith(str)){
					String[] subStrs = str.trim().split(",");
					AppInfo appInfo = new AppInfo();
					appInfo.app_num = subStrs[0];
					appInfo.app_subtype_name = subStrs[1]; 
					appInfos.put(appInfo.app_subtype_name, appInfo);
				}
				
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			sc.close();
		}
    	
    	
    }
    
    /**
     * only for test
     * @param args
     */
    public static void main(String args[]){
		String protoList = "G:\\项目材料\\2021-AiRules\\参考文档\\8.0 计划发布\\proto.list";
    	ProtoList p = new ProtoList(protoList);
    	for(Entry<String, AppInfo> entry : p.appInfos.entrySet()){
    		AppInfo appInfo = (AppInfo) entry.getValue();
    		String str = entry.getKey()+","+appInfo.app_num+"|"+appInfo.getNum_type()+"|"+appInfo.getNum_subtype()+"|"+appInfo.getNum_content()+","+appInfo.app_subtype_name;
    		System.out.println(str);
    	}
    }

}
